package com.example.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;
import com.example.gulimall.product.dao.BrandDao;
import com.example.gulimall.product.dao.CategoryBrandRelationDao;
import com.example.gulimall.product.dao.CategoryDao;
import com.example.gulimall.product.entity.BrandEntity;
import com.example.gulimall.product.entity.CategoryBrandRelationEntity;
import com.example.gulimall.product.entity.CategoryEntity;
import com.example.gulimall.product.service.BrandService;
import com.example.gulimall.product.service.CategoryBrandRelationService;
import com.example.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    CategoryBrandRelationDao relationDao;
    @Autowired
    BrandService brandService;
    @Autowired
    BrandDao brandDao;
    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取分类关联的品牌
     *
     * @param catId
     * @return java.util.List<com.example.gulimall.product.entity.BrandEntity>
     * @author GuLin
     * @date 14:07 2021/6/8
     **/
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> entities =
                relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandEntity> brandList = entities.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
        return brandList;
    }

    /**
     * 新增品牌与分类关联关系
     *
     * @param categoryBrandRelation
     * @return void
     * @author GuLin
     * @date 14:09 2021/6/8
     **/
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //根据品牌ID查询品牌名称
        Long brandId = categoryBrandRelation.getBrandId();
        //根据分类ID查询分类名称
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if (null != brandEntity) {
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }
        if (null != categoryEntity) {
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }

        CategoryBrandRelationEntity entity = relationDao.selectOne(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId).eq("catelog_id", catelogId));
        if (null != entity) {
            categoryBrandRelation.setId(entity.getId());
            this.updateById(categoryBrandRelation);
        } else {
            this.save(categoryBrandRelation);
        }
    }

    @Override
    public void updateCategory(Long catId, String name) {
       CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
       entity.setCatelogId(catId);
       entity.setCatelogName(name);
       this.update(entity,new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
    }
}